package com.huluobo.higt4demo1911.ui.video;

import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.huluobo.higt4demo1911.R;
import com.huluobo.higt4demo1911.adapter.VideoFullScreenAdapter;
import com.huluobo.higt4demo1911.bean.VideoBean;
import com.huluobo.higt4demo1911.contract.VideoContract;
import com.huluobo.higt4demo1911.core.mvp.BaseFragment;
import com.huluobo.higt4demo1911.presenter.VideoPresenter;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.shuyu.gsyvideoplayer.video.base.GSYVideoView;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * Created by lc on 2022/4/27.
 */
public class VideoFullScreenFragment extends BaseFragment<VideoPresenter> implements VideoContract.View {
    private RecyclerView recyclerView;
    private VideoFullScreenAdapter mAdapter;
    private LinearLayoutManager linearLayoutManager;
    private PagerSnapHelper snapHelper;

    @Override
    public void initAdapter(List<VideoBean> videoBeans) {
        if (mAdapter == null) {
            mAdapter = new VideoFullScreenAdapter(videoBeans);
            recyclerView.setAdapter(mAdapter);
        }
    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_video_cart_list;
    }

    @Override
    public void initView() {
        recyclerView = findViewById(R.id.fragment_video_rv);
        recyclerView.setLayoutManager(linearLayoutManager = new LinearLayoutManager(getContext()));
        //处理Recyclerview在手势离开屏幕后,惯性滑动结束,整页item停留位置
        snapHelper = new PagerSnapHelper();
        snapHelper.attachToRecyclerView(recyclerView);
        //子item绑定状态改变时的回调
        recyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(@NonNull @NotNull View view) {
                Log.i("TagA", "onChildViewAttachedToWindow:" + view.getTransitionName());
            }

            @Override
            public void onChildViewDetachedFromWindow(@NonNull @NotNull View view) {
                Log.i("TagA", "onChildViewDetachedFromWindow:" + view.getTransitionName());
            }
        });
        //监听滑动状态
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull @NotNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                //切入点,状态为0时开始播放
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    //空闲状态
                    View snapView = snapHelper.findSnapView(linearLayoutManager);
                    int position = linearLayoutManager.getPosition(snapView);
                    StandardGSYVideoPlayer player = (StandardGSYVideoPlayer) mAdapter
                            .getViewByPosition(position, R.id.item_full_screen_video_player);
                    if (player.getCurrentState() != GSYVideoView.CURRENT_STATE_PLAYING) {
                        player.startPlayLogic();
                    }
                }
            }

            @Override
            public void onScrolled(@NonNull @NotNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });

    }

    @Override
    public void initData() {
        mPresenter = new VideoPresenter(this);
        mPresenter.list();
    }
}
